package com.translate.web.service.impl;

import com.translate.web.dao.ChangeRecordMapper;
import com.translate.web.param.contract.IDateRangeParam;
import com.translate.web.param.contract.IPayBehaviorRequestParam;
import com.translate.web.param.contract.IUserIdParam;
import com.translate.entity.ChangeRecord;
import com.translate.web.service.IChangeRecordService;
import com.basic.service.impl.BaseServiceImpl;
import com.param.contract.IPage;

import java.math.BigDecimal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ChangeRecordServiceImpl extends BaseServiceImpl<ChangeRecord> implements IChangeRecordService {
	public ChangeRecordServiceImpl(ChangeRecordMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(ChangeRecord data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(ChangeRecord data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<ChangeRecord> getEntityClass() {
		return ChangeRecord.class;
	}
	
	protected <S extends IPage> String sqlByGetCounttAfterProcesser(S param) {
		
		IDateRangeParam userIdParam = (IDateRangeParam)param;
		
		return this.sqlByGetCount(" user_id = " + userIdParam.getUserId());
	}
	
	protected <S extends IPage> String sqlByGetPageListAfterProcesser(S param) {
			
		IDateRangeParam userIdParam = (IDateRangeParam)param;
        
        return this.sqlByGetPageList(" and user_id = " + userIdParam.getUserId()+" order by create_time desc");
	}
	
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int addChangeRecord(IPayBehaviorRequestParam behaviorRequestParam, BigDecimal money) {
		
		ChangeRecord changeRecord = new ChangeRecord();
		
		changeRecord.setUserId((int) behaviorRequestParam.getUserId());
		
		changeRecord.setChangeType((byte) 0);
		
		changeRecord.setReason("购买服务");
		
		changeRecord.setMoney(money);
		
		insertPropteris(changeRecord);
		
		return this.insert(changeRecord);
	}

}